<script>
	import {
		mapMutations
	} from 'vuex';
	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.gettoken()
			/* let scene = decodeURIComponent(e.query.scene)
			// console.log("onLaunch scene", scene)
			if (scene && scene != "undefined") {
				uni.setStorageSync('parentId', decodeURIComponent(scene));
			} */
		},
		onShow: function() {
			console.log('App Show')
			/* let scene = decodeURIComponent(e.query.scene)
			// console.log("onShow scene", scene)
			if (scene && scene != "undefined") {
				uni.setStorageSync('parentId', decodeURIComponent(scene));
			} */
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			...mapMutations(['login']),
			async gettoken() {
				let that = this
				let openid = ""
				// console.log("gettoken")
				//#ifdef MP-WEIXIN
				const js_code = await new Promise((resolve, reject) => {
					// uni.login非异步，所以用Promise包装  
					uni.login({
						provider: 'weixin',
						success: res => {
							resolve(res.code);
						},
						fail: err => {
							reject(err);
						}
					});
				})
				const [error, res] = await uni.request({
					url: that.baseUrl + "/wechat/ma/login",
					data: {
						code: js_code
					}
				})
				openid = res.data.data.openid
				//#endif
				// console.log("wedata", that.wedata)
				//#ifdef H5
				openid = "o8cWN4v1J7Bup7H9u4N1EsUNmZTM" //测试用
				//#endif
				// openid = "oYAAC5oN-FPPGRoqj34rw704DYFA1231334254d" //测试用
				// openid = "oYAAC5hzqO2e9Cy-gEUPULUZCS9U" //测试用
				// openid = "oYAAC5huVdUGa6UgR84-0sVl5fqA" //项目经理
				// openid = "oYAAC5trg8W6Z4O1s38CAojFyhOo" //设计师
				// openid = "oYAAC5jWd-oiXUWJGVd5t5BSm5-A" //材料商
				// console.log("openid", openid)
				if (openid) {
					uni.request({
						url: that.baseUrl + "/api/login",
						data: {
							openId: openid
						},
						header: {
							"Content-Type": "application/x-www-form-urlencoded"
						},
						method: 'POST',
						success(r) {
							// console.log("登录结果", r)
							if (r.data.code == 0) {
								that.login({
									token: r.data.data.token,
									nickName: r.data.data.user
										.nickName,
									phone: r.data.data.user.phone,
									headimg: r.data.data.user
										.avatar,
									openId: openid,
									typeId: r.data.data.user.typeId,
									id: r.data.data.user.memberId,
									jmsStatus: r.data.data.user.sex,
									plotId: r.data.data.user.plotId,
									plotName: r.data.data.user.plotName
									// jmsStatus: 1,
								})
								// that.$public.reload()
							}
						}
					})
				}

			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	view,
	scroll-view,
	swiper,
	swiper-item,
	cover-view,
	cover-image,
	icon,
	text,
	rich-text,
	progress,
	button,
	checkbox,
	form,
	input,
	label,
	radio,
	slider,
	switch,
	textarea,
	navigator,
	audio,
	camera,
	image,
	video {
		box-sizing: border-box;
	}

	.container {
		padding-bottom: calc(24rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
	}

	.content {
		background: none;
	}

	.divider {
		background: #E0E3DA;
		width: 100%;
		height: 1rpx;
	}

	.btncss {
		background: linear-gradient(to right, #DF686A, #EE8D56);
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		border-radius: 50rpx;
		font-size: 34rpx;
		font-weight: 600;
		letter-spacing: 20rpx;
	}

	// .container {
	// 	padding-top: 10rpx;
	// }

	.bg-colore {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		background-color: #f5f5f5;
		z-index: -1;
	}

	.iconmar {
		margin-left: 10rpx; 
	}
</style>